/**
 * 项目名：phms3
 * 日期：    2019-11-22 16:35:44
 * Copyright (c) 2015- HTSC-版权所有
 */

package com.joysuch.wwyt.core.repository;

import java.util.List;

import com.joysuch.wwyt.core.entity.BaseTypeGroupItem;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

/**
 * BaseBizCodeSeedDao
 */
public interface BaseTypeGroupItemDao
        extends JpaRepository<BaseTypeGroupItem, Long>, QuerydslPredicateExecutor<BaseTypeGroupItem> {
    public Page<BaseTypeGroupItem> findAll(Specification<BaseTypeGroupItem> spec, Pageable pageable);

    public BaseTypeGroupItem save(BaseTypeGroupItem bean);

    public void delete(BaseTypeGroupItem bean);

    public BaseTypeGroupItem findFirstByCode(String code);

    @Query("select name from BaseTypeGroupItem where id = ?1")
    public String findNameById(Long groupId);

    @Query("select name from BaseTypeGroupItem where id = ?1")
    String findNameByIdAndState(Long groupId);
    
    @Query(value = "select name from wwyt_base_type_group_item where id = ?1  AND delete_flag = '0' AND (state is null or state = 1) ", nativeQuery = true)
    String findNameByIdAndDeleteFlag(Long id);

    @Query("select name from BaseTypeGroupItem where id in (?1)")
    public List<String> findNameByIdIn(List<Long> groupId);

    @Query("select name from BaseTypeGroupItem where id = ?1")
    public String getNameById(Long reason);

    public BaseTypeGroupItem findNameByCode(String code);

    public BaseTypeGroupItem findNameByCodeAndTypeGroupId(String code, Long groupTypeId);

    @Query(value = "SELECT Id FROM wwyt_base_type_group_item WHERE NAME =?1 AND TYPE_GROUP_ID =?2  AND delete_flag = '0' AND (state is null or state = 1)", nativeQuery = true)
    public Long findIdByName(String name, Long typeId);

    @Query(value = "SELECT * FROM wwyt_base_type_group_item WHERE NAME =?1 AND delete_flag = '0' AND (stete is null or state = 1)", nativeQuery = true)
    List<BaseTypeGroupItem> findByName(String name);

    @Query(value = "SELECT * FROM wwyt_base_type_group_item WHERE code like :code%  AND delete_flag = '0'", nativeQuery = true)
    List<BaseTypeGroupItem> getListByCodeLike(@Param("code") String code);

    @Query(value = "SELECT * FROM wwyt_base_type_group_item WHERE TYPE_GROUP_ID =?1  AND delete_flag = '0' AND (state is null or state = 1)", nativeQuery = true)
    List<BaseTypeGroupItem> findByTypeGroupId(Long id);

    @Query(value = "SELECT * FROM wwyt_base_type_group_item WHERE NAME =?1 AND TYPE_GROUP_ID =?2  AND delete_flag = '0' AND (state is null or state = 1)", nativeQuery = true)
    List<BaseTypeGroupItem> findByNameAndTypeGroupId(String name, Long typeGroupId);

    @Query(value = "select * from wwyt_base_type_group_item where NAME =?2 AND TYPE_GROUP_ID =?1 AND delete_flag = '0' AND (state is null or state = 1)" , nativeQuery = true)
    BaseTypeGroupItem findFirstByTypeGroupIdAndName(Long typeGroupId,String name);
    
    @Modifying
    @Transactional
    @Query(value = " update wwyt_base_type_group_item set state = '1' , delete_flag = '1' where TYPE_GROUP_ID = ?1 ", nativeQuery = true)
    void deleteAllByTypeGroupIdIn(Long groupId);
}